package com.lxw.weather.activity;

import android.os.Bundle;


import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.lxw.weather.R;
import com.lxw.weather.adapter.DailyForecastAdapter;
import com.lxw.weather.model.DailyForecast;
import java.util.ArrayList;
import java.util.List;

public class ForecastActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private DailyForecastAdapter adapter;
    private List<DailyForecast> forecast7dList = new ArrayList<>();
    private List<DailyForecast> forecast15dList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forecast);

        // 初始化RecyclerView（传递15天标志）
        recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new DailyForecastAdapter(false);  // 默认显示7天（非15天）
        recyclerView.setAdapter(adapter);

        // 加载数据
        Bundle bundle = getIntent().getBundleExtra("forecastBundle");
        if (bundle != null) {
            forecast7dList = bundle.getParcelableArrayList("forecast7dData");
            forecast15dList = bundle.getParcelableArrayList("forecast15dData");
        }
        if (forecast7dList == null) forecast7dList = new ArrayList<>();
        if (forecast15dList == null) forecast15dList = new ArrayList<>();

        // 默认显示7天预报
        adapter.setForecasts(forecast7dList);

        // 按钮点击逻辑（关键：切换时更新适配器的15天标志）
        findViewById(R.id.btn7Days).setOnClickListener(v -> {
            adapter = new DailyForecastAdapter(false);  // 7天模式
            recyclerView.setAdapter(adapter);
            adapter.setForecasts(forecast7dList);
        });

        findViewById(R.id.btn15Days).setOnClickListener(v -> {
            adapter = new DailyForecastAdapter(true);  // 15天模式
            recyclerView.setAdapter(adapter);
            adapter.setForecasts(forecast15dList);
        });
    }
}